import Setting from '@/setting.env'
import { getToken } from '@/utils/auth'
import store from "@/store";

/**
 * 判断是否存在域名
 * @param {Object} url 链接
 */
export function checkURL(url) {
  url = url || '';
  let http;
  if (url.substr(0, 7).toLowerCase() == 'http://') http = url.substr(0, 7).toLowerCase();
  else if (url.substr(0, 8).toLowerCase() == 'https://') http = url.substr(0, 8).toLowerCase();

  if (http) {
    let str = http + url.split('/')[2];
    let Expression = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
    let objExp = new RegExp(Expression);
    if (objExp.test(str) == true){
      return url;
    };
  };
  return Setting.PICTURE_PATH +'/admin'+ url;
}

/**
 * * 决JavaScript split方法出现空字符的问题
 * @param {Object} val 值 类型：字符串
 * @param {Object} key 分隔符
 */
export function splitArray(val, key) {
  return val.split(key).filter(item => item != '');
}

/**
 * 将时间解析为字符串
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string | null}
 */
export function parseTime(time, cFormat) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string')) {
      if ((/^[0-9]+$/.test(time))) {
        // support "1548221490638"
        time = parseInt(time)
      } else {
        // support safari
        // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
        time = time.replace(new RegExp(/-/gm), '/')
      }
    }

    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

/**
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time, option) {
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000
  } else {
    time = +time
  }
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return (
      d.getMonth() +
      1 +
      '月' +
      d.getDate() +
      '日' +
      d.getHours() +
      '时' +
      d.getMinutes() +
      '分'
    )
  }
}

/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
  const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  if (!search) {
    return {}
  }
  const obj = {}
  const searchArr = search.split('&')
  searchArr.forEach(v => {
    const index = v.indexOf('=')
    if (index !== -1) {
      const name = v.substring(0, index)
      const val = v.substring(index + 1, v.length)
      obj[name] = val
    }
  })
  return obj
}

/**
 * 数组排序
 * @param key 数组的key
 * @param isAsc 降序/排序
 * @returns {Function}
 */
export function sortExp(key, isAsc) {
  return function(x, y) {
    if (isNaN(key)) {
      if (x[key] > y[key]) {
        return 1 * (isAsc ? 1 : -1)
      } else if (x[key] < y[key]) {
        return -1 * (isAsc ? 1 : -1)
      } else {
        return 0
      }
    } else {
      return (x[key] - y[key]) * (isAsc ? 1 : -1)
    }
  }
}

/**
 * 判断字符串的结尾的字符串
 * @param str
 * @param target
 * @returns {boolean}
 */
export function confirmEnding(str, target) {
  // 请把你的代码写在这里
  var start = str.length - target.length
  var arr = str.substr(start, target.length)
  // eslint-disable-next-line eqeqeq
  if (arr == target) {
    return true
  }
  return false
}
